import { action, observable } from "mobx"
import { getUserInfo, getAuthCode, changeMobile,changeInfo } from "../../services/mine"

class Mine {
    @observable
    myList = {}

    @observable
    phone = ''

    @observable
    code = ''

    @observable
    num = 60

    @action
    async getMyList() {
        let list = await getUserInfo()
        let str = list.data.mobile
        let strs = (str as string).slice(4, 8)
        str = (str as string).replace(strs, '****')
        let obj = {
            ...list.data,
            mobile: str
        }
        this.myList = obj
    }

    @action
    async getMyPhone(phone:string) {
        this.phone = phone
        let codes = await getAuthCode(phone)
        console.log(codes);
        let time = setInterval(() => {
            this.num--
            if (this.num === 0) {
                clearInterval(time)
                this.num = 60
            }
        }, 1000)
        this.code = codes.data.authCode
    }

    @action
    async setPhone(code:string){
        if(this.code===code){
            let obj={
                mobile: this.phone,
                authCode:this.code,
                mid:'fa291cbd52aa40d2b47ba96c8ec59c99',
            }
            console.log(obj);
           await changeMobile(obj)
        }
    }

    @action setName(name:string){
        let obj={
            name:name,
            mid:'fa291cbd52aa40d2b47ba96c8ec59c99',
        }
        changeInfo(obj)
    }
}
export default Mine